<?php
session_start();
require 'common/Utility.php';
require 'crud/CustomSecurity.class.php';
require 'crud/Users.class.php';
require 'db/configuration.php';
if(!isBrowserSupported()){
		header("Location: supportedBrowsers.php");
	} 
	rediectIfLogged();
    if (isset($_POST['email'])){
    	$validUser = isValidUser(escape_string($_POST['email']), md5($_POST['password']));
    	if ($validUser == 1){
    		if($_POST['save'] != null){
    			$siteName = getSiteName();
    			setcookie($siteName."[usr]", $_POST['email'], time()+60*60*24*30);
    			setcookie($siteName."[pwd]",  md5($_POST['password']), time()+60*60*24*30);
    		}
    		gotoRequestedURL();
//    		header("Location: main.php");
    	} else if ($validUser == 0) {
    		$status = '<div class="message"> Грешно име/парола </div>';
    	} else {
    		$status = '<div class="message"> Потребителя не е активиран. Проверете пощата си и последвайте връзката. Ако нямате писмо поискйте ново от: <a href="activate.php?request='.$_POST['email'].'"> тук.</а> </div>';
    	}
    } ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 
<link rel="stylesheet" type="text/css" href="css/login.css"/>
<link rel="stylesheet" type="text/css" href="css/page.css"/>
<script type="text/javascript" src="js/textevents.js"></script>
<title>Log in</title>

</head>
    <body>


<div id="lenta">
<ul>
	<li><input id="bugs" type="button" value="" onclick="location.href='http://code.google.com/p/rynyty/issues/list';"></input></li>
	<li><a class="lenta" href="main.php"> начало </a></li>
	<?php
	if (isset($_SESSION['user'])){
		echo '<li><a class="lenta-logout" href="logout.php">Hui out '.$_SESSION['user'].'</a></li>';
	} else {
		echo '<li><a class="lenta" href="register.php">register</a></li>';
		echo '<li><a class="lenta" href="login.php">login</a></li>';
	}
	?>
</ul>
</div>

<div id="main-login">



<?php
global $status;
if (isset($status)){
	echo $status;
} 
?>

<div id="login">

    <form action="login.php" method ="POST">
<ol> 
 
<li> 
<!-- <label for="email" id="label-email">email</label>   -->
<input id="email" type="text" name="email" value="<?= getEmail()?>" onfocus="putStringOnTextFocus('email','email');" onblur="putStringOnTextBlur('email','email');">
</li>

<li>
<!--  <label for="password" id="label-password">password</label> -->
<div id="password_dummy">
<input id="dummy" class="password" type="text" name="password" size="15" maxlength="14" value="парола" onfocus="changeToPassword();">
</div>
<div id="password_div" style="display:none">
<input id="password" type="password" name="password" size="15" maxlength="14" value="" onblur="changeToText();">
</div>
</li>

<li>  
<div id="checkbox-div">
<input type="checkbox" checked name="save" value="true">
<label for="checkbox" id="label-checkbox" >запомни ме</label>
</div>
</li>

<li class="submit" >
<div id="submit-div">
<input id="submit" type="submit" name = "login" value="">
</div>
</li>

<li>
<div id="register">
<a class="lenta" href="register.php">регистрирай се</a> 
</div>
</li>

</ol>
    </form>

</div>
</div>
<?php 
function getEmail(){
	if (strlen(getPost('email')) == 0) {
		return 'email'; 
	} 
	return getPost('email');	
}
?>
    </body>
</html>
